//
//  StrategyViewController.m
//  ITrip
//
//  Created by dlios on 15-5-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "StrategyViewController.h"
#import "StrategySecondViewController.h"
#import "StrategySecondInlandViewController.h"
#import "StrategySecondNearbyViewController.h"
#import "SetViewController.h"
@interface StrategyViewController ()<sendValue, sendInlandValue, sendNearbyValue, UIAlertViewDelegate>

@end

@implementation StrategyViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    //装饰导航栏
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    title.text = @"旅行攻略";
    title.font = [UIFont systemFontOfSize:20];
    title.font = [UIFont boldSystemFontOfSize:25];
    title.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = title;
    
    
    self.arrayData = [NSMutableArray array];
    
    //分段控件
    NSArray *arrayControl = [NSArray arrayWithObjects:@"国外", @"国内", @"附近", nil];
    seg = [[UISegmentedControl alloc]initWithItems:arrayControl];
    seg.frame = CGRectMake(12.5, 10, self.view.frame.size.width - 25, 35);
    [self.view addSubview:seg];
    seg.tintColor = [UIColor grayColor];
    //设置初始化的第一个界面为0
    seg.selectedSegmentIndex = 0;
    [seg addTarget:self action:@selector(clickSegControl:) forControlEvents:UIControlEventValueChanged];
    [seg release];

    
    //创建uiscrollview
    myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 64)];
    myScroll.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    [self.view addSubview:myScroll];
    myScroll.contentSize = CGSizeMake(self.view.frame.size.width * 3, self.view.frame.size.height - 64);
  
    
    //菊花控件
    activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.color = [UIColor grayColor];
    [self.view addSubview:activity];
    [activity release];
    [activity startAnimating];
    activity.center = self.view.center;
    NSString *path = [URLPath strategyURL];

    //下载
    //创建一个AFN管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //告诉AFN不要解析
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = @{@"page" : @"1"};
    [manager GET:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arrayData = [StrategyParser getParser:operation.responseData];

        //菊花控件停止
        [activity stopAnimating];
        
        [self initTableView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败" message:nil delegate:self cancelButtonTitle:@"重新连接" otherButtonTitles:nil, nil];
        [activity stopAnimating];
        alert.delegate = self;
        [alert show];
    }];
}

- (void) viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.tabBar.translucent = NO;
    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    //菊花控件
    activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.color = [UIColor grayColor];
    [self.view addSubview:activity];
    [activity release];
    [activity startAnimating];
    activity.center = self.view.center;
    NSString *path = [URLPath strategyURL];
    
    //下载
    //创建一个AFN管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //告诉AFN不要解析
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = @{@"page" : @"1"};
    [manager GET:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arrayData = [StrategyParser getParser:operation.responseData];
        
        //菊花控件停止
        [activity stopAnimating];
        
        [self initTableView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败" message:nil delegate:self cancelButtonTitle:@"重新连接" otherButtonTitles:nil, nil];
        [activity stopAnimating];
        alert.delegate = self;
        [alert show];
    }];
    
    
}


- (void)initTableView
{

    //添加三个自定义的view（国外，国内，附近）
    myForeign = [[ForeignView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    myInland = [[InlandView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
    myNearby = [[NearbyView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 2, 0, self.view.frame.size.width, self.view.frame.size.height )];
    myForeign.delegate = self;
    myInland.Inlanddelegate = self;
    myNearby.delegate = self;

    
    [myScroll addSubview:myForeign];
    [myScroll addSubview:myInland];
    [myScroll addSubview:myNearby];
    
//    [myForeign release];
//    [myInland release];
//    [myNearby release];
    
    //取消滚动条
    myScroll.showsHorizontalScrollIndicator = NO;
    myScroll.showsVerticalScrollIndicator = NO;
    myScroll.pagingEnabled = YES;
    
    //偏移量
//    [myScroll setContentOffset:CGPointMake(0, 0) animated:NO];
    myScroll.delegate = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    myScroll.bounces = NO;
    myScroll.alwaysBounceHorizontal = NO;
    myScroll.alwaysBounceVertical = YES;
    
    [myScroll release];
}



//跳转到第一个页面的代理传值
- (void)sendValue:(NSInteger)section :(NSInteger)row
{
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.tabBar.translucent = YES;
    StrategySecondViewController *second = [[StrategySecondViewController alloc] init];
    NSArray *array = [self.arrayData objectAtIndex:section];
    StrategyModel *model = [array objectAtIndex:row];
    second.number = model.id;
    
    second.titleName = model.name_zh_cn;
    [self.navigationController pushViewController:second animated:YES];
}

//跳转到第二个页面的代理传值
- (void)sendInlandValue:(NSInteger)section :(NSInteger)row :(NSInteger)inLandId :(NSString *)name
{
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.tabBar.translucent = YES;
    StrategySecondInlandViewController *secondInland = [[StrategySecondInlandViewController alloc]init];
    
    secondInland.numberInland = inLandId;
    
    secondInland.titleName1 = name;
    
    [self.navigationController pushViewController:secondInland animated:YES];
}

//跳转到第三个界面的代理传值
- (void)sendNearbyValue:(NSInteger)row :(NSInteger)nearbyID :(NSString *)name
{
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.tabBar.translucent = YES;
    StrategySecondNearbyViewController *secondNearby = [[StrategySecondNearbyViewController alloc]init];
    secondNearby.numberNearby = nearbyID;
    secondNearby.titleName = name;
    [self.navigationController pushViewController:secondNearby animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger i = myScroll.contentOffset.x / self.view.frame.size.width;
    seg.selectedSegmentIndex = i;
}

//seg点击方法的实现
- (void)clickSegControl:(UISegmentedControl *)segControl
{
    NSInteger i = segControl.selectedSegmentIndex;
    [myScroll setContentOffset:CGPointMake(self.view.frame.size.width * i, 0) animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
